package com.heartech.prediciton_wx.controller.qrstr;

import com.heartech.prediciton_wx.constant.WxCode;
import com.heartech.prediciton_wx.entity.qrcode.QrStrState;
import com.heartech.prediciton_wx.service.qrcode.QrCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@RestController
@RequestMapping("/wxpublic")
public class GetStrController {

    @Autowired
    private QrCodeService qrCodeService;

    @GetMapping("/get_str/")
    public ResponseEntity<QrStrState> strGet(@RequestParam String random_str) {
        try {
            log.info("Processing GET request for random_str: {}", random_str);

            if (random_str == null || random_str.trim().isEmpty()) {
                log.warn("Invalid request parameter: random_str is empty");
                return ResponseEntity.badRequest().build();
            }

            String url = WxCode.get_str + "?random_str=" + random_str;
            QrStrState response = qrCodeService.sendGetRequest(url);

            if (response != null) {
                return ResponseEntity.ok(response);
            } else {
                log.error("Service returned null response for random_str: {}", random_str);
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }
        } catch (Exception e) {
            log.error("Error occurred while processing GET request: ", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
}

